﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Permutator
{
    class Permutations
    {
        protected char[] m_Alphabet;
        protected int m_Length;
        protected int m_Size;
        public Permutations(char[] alphabet, int length)
        {
            m_Alphabet = alphabet;
            m_Length = length;
            m_Size = (int)Math.Pow(m_Alphabet.Length, m_Length);
        }
        public int Length
        {
            get { return m_Size; }
        }
        public string this[int index]
        {
            get
            {
                if (index < 0 || index > Length)
                {
                    throw new ArgumentOutOfRangeException();
                }
                string result = string.Empty;
                int rest = 0;
                for (int i = m_Length - 1; i >= 0; i--)
                {
                    rest = index % m_Alphabet.Length;
                    index = index / m_Alphabet.Length;
                    result = m_Alphabet[rest] + result;
                }
                return result;
            }
        }
    }
}
